package com.wzt.greenhouse.model;


import com.wzt.greenhousemobile.DPWebService;
import com.wzt.greenhousemobile.HardwareManager;
import com.wzt.greenhousemobile.R;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class ControlInfo {
	
	String name;
	String state;
	ControlButton onButton;
	ControlButton upButton;
	ControlButton downButton;
	ControlButton offButton;
	ImageView imageView = null;
	String id;
	String comnum;
	String comstop;
	String comdown;
	
	DrawBackupAnimation animation = new DrawBackupAnimation();
	int image;
	int type;
	int imageGroup =0;
	
	public ControlInfo(String strName,int nImage,int nType) {
		// TODO Auto-generated constructor stub
		name = strName;
		image = nImage;
		type = nType;
		state = "stop";
	}
	
	public ControlInfo(String strName,String id) {
		// TODO Auto-generated constructor stub
		name = strName;
		this.id = id;
		state = "stop";
		
		onButton = new ControlButton(this);
		upButton = new ControlButton(this);
		downButton = new ControlButton(this);
		offButton = new ControlButton(this);
	}

	public String getComdown() {
		return comdown;
	}
	
	public String getComnum() {
		return comnum;
	}
	
	public String getComstop() {
		return comstop;
	}
	
	public void setComdown(String comdown) {
		this.comdown = comdown;
	}
	
	public void setComnum(String comnum) {
		this.comnum = comnum;
	}
	
	public void setComstop(String comstop) {
		this.comstop = comstop;
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
	public void resetAnimation()
	{
		animation.stopAnimation();
		animation.animations.clear();
		switch (type) {
		case 1:
			if(state.equals("stop") || state.equals("upstop") || state.equals("downstop"))
			{
				HardwareManager.AnimationRemove(this);
			}
			if(state.equals("upping"))
			{
				switch(imageGroup)
				{
				case 1:
					animation.addImage(R.drawable.jm_1_1);
					animation.addImage(R.drawable.jm_1_2);
					animation.addImage(R.drawable.jm_1_3);
					animation.addImage(R.drawable.jm_1_4);
					animation.addImage(R.drawable.jm_1_5);
					animation.addImage(R.drawable.jm_1_6);
					animation.addImage(R.drawable.jm_1_7);
					animation.addImage(R.drawable.jm_1_8);
					animation.addImage(R.drawable.jm_1_9);
					animation.addImage(R.drawable.jm_1_10);
					break;
				case 2:
					animation.addImage(R.drawable.jm_2_1);
					animation.addImage(R.drawable.jm_2_2);
					animation.addImage(R.drawable.jm_2_3);
					animation.addImage(R.drawable.jm_2_4);
					animation.addImage(R.drawable.jm_2_5);
					animation.addImage(R.drawable.jm_2_6);
					animation.addImage(R.drawable.jm_2_7);
					animation.addImage(R.drawable.jm_2_8);
					animation.addImage(R.drawable.jm_2_9);
					animation.addImage(R.drawable.jm_2_10);
					break;
				case 3:
					animation.addImage(R.drawable.jm_3_1);
					animation.addImage(R.drawable.jm_3_2);
					animation.addImage(R.drawable.jm_3_3);
					animation.addImage(R.drawable.jm_3_4);
					animation.addImage(R.drawable.jm_3_5);
					animation.addImage(R.drawable.jm_3_6);
					animation.addImage(R.drawable.jm_3_7);
					animation.addImage(R.drawable.jm_3_8);
					animation.addImage(R.drawable.jm_3_9);
					animation.addImage(R.drawable.jm_3_10);
					break;
				case 4:
					animation.addImage(R.drawable.jm_4_1);
					animation.addImage(R.drawable.jm_4_2);
					animation.addImage(R.drawable.jm_4_3);
					animation.addImage(R.drawable.jm_4_4);
					animation.addImage(R.drawable.jm_4_5);
					animation.addImage(R.drawable.jm_4_6);
					animation.addImage(R.drawable.jm_4_7);
					animation.addImage(R.drawable.jm_4_8);
					animation.addImage(R.drawable.jm_4_9);
					animation.addImage(R.drawable.jm_4_10);
					break;
				case 5:
					animation.addImage(R.drawable.jm_5_1);
					animation.addImage(R.drawable.jm_5_2);
					animation.addImage(R.drawable.jm_5_3);
					animation.addImage(R.drawable.jm_5_4);
					animation.addImage(R.drawable.jm_5_5);
					animation.addImage(R.drawable.jm_5_6);
					animation.addImage(R.drawable.jm_5_7);
					animation.addImage(R.drawable.jm_5_8);
					animation.addImage(R.drawable.jm_5_9);
					animation.addImage(R.drawable.jm_5_10);
					break;
				case 6:
					animation.addImage(R.drawable.jm_6_1);
					animation.addImage(R.drawable.jm_6_2);
					animation.addImage(R.drawable.jm_6_3);
					animation.addImage(R.drawable.jm_6_4);
					//animation.addImage(R.drawable.jm_6_5);
					animation.addImage(R.drawable.jm_6_6);
					animation.addImage(R.drawable.jm_6_7);
					animation.addImage(R.drawable.jm_6_8);
					animation.addImage(R.drawable.jm_6_9);
					animation.addImage(R.drawable.jm_6_10);
					break;
				default:
					break;
				}
			}
			if(state.equals("downing"))
			{
				switch (this.imageGroup) {
				case 1:
					animation.addImage(R.drawable.jm_1_10);
					animation.addImage(R.drawable.jm_1_9);
					animation.addImage(R.drawable.jm_1_8);
					animation.addImage(R.drawable.jm_1_7);
					animation.addImage(R.drawable.jm_1_6);
					animation.addImage(R.drawable.jm_1_5);
					animation.addImage(R.drawable.jm_1_4);
					animation.addImage(R.drawable.jm_1_3);
					animation.addImage(R.drawable.jm_1_2);
					animation.addImage(R.drawable.jm_1_1);
					break;
				case 2:
					animation.addImage(R.drawable.jm_2_10);
					animation.addImage(R.drawable.jm_2_9);
					animation.addImage(R.drawable.jm_2_8);
					animation.addImage(R.drawable.jm_2_7);
					animation.addImage(R.drawable.jm_2_6);
					animation.addImage(R.drawable.jm_2_5);
					animation.addImage(R.drawable.jm_2_4);
					animation.addImage(R.drawable.jm_2_3);
					animation.addImage(R.drawable.jm_2_2);
					animation.addImage(R.drawable.jm_2_1);
					break;
				case 3:
					animation.addImage(R.drawable.jm_3_10);
					animation.addImage(R.drawable.jm_3_9);
					animation.addImage(R.drawable.jm_3_8);
					animation.addImage(R.drawable.jm_3_7);
					animation.addImage(R.drawable.jm_3_6);
					animation.addImage(R.drawable.jm_3_5);
					animation.addImage(R.drawable.jm_3_4);
					animation.addImage(R.drawable.jm_3_3);
					animation.addImage(R.drawable.jm_3_2);
					animation.addImage(R.drawable.jm_3_1);
					break;
				case 4:
					animation.addImage(R.drawable.jm_4_10);
					animation.addImage(R.drawable.jm_4_9);
					animation.addImage(R.drawable.jm_4_8);
					animation.addImage(R.drawable.jm_4_7);
					animation.addImage(R.drawable.jm_4_6);
					animation.addImage(R.drawable.jm_4_5);
					animation.addImage(R.drawable.jm_4_4);
					animation.addImage(R.drawable.jm_4_3);
					animation.addImage(R.drawable.jm_4_2);
					animation.addImage(R.drawable.jm_4_1);
					break;
				case 5:
					animation.addImage(R.drawable.jm_5_10);
					animation.addImage(R.drawable.jm_5_9);
					animation.addImage(R.drawable.jm_5_8);
					animation.addImage(R.drawable.jm_5_7);
					animation.addImage(R.drawable.jm_5_6);
					animation.addImage(R.drawable.jm_5_5);
					animation.addImage(R.drawable.jm_5_4);
					animation.addImage(R.drawable.jm_5_3);
					animation.addImage(R.drawable.jm_5_2);
					animation.addImage(R.drawable.jm_5_1);
					break;
				case 6:
					animation.addImage(R.drawable.jm_6_10);
					animation.addImage(R.drawable.jm_6_9);
					animation.addImage(R.drawable.jm_6_8);
					animation.addImage(R.drawable.jm_6_7);
					animation.addImage(R.drawable.jm_6_6);
					//animation.addImage(R.drawable.jm_6_5);
					animation.addImage(R.drawable.jm_6_4);
					animation.addImage(R.drawable.jm_6_3);
					animation.addImage(R.drawable.jm_6_2);
					animation.addImage(R.drawable.jm_6_1);
					break;
				default:
					break;
				}
			}
			break;
		case 2:
			if(state.equals("stop"))
			{
				HardwareManager.AnimationRemove(this);
			}
			if(state.equals("upping"))
			{
				animation.addImage(R.drawable.jl_1);
				animation.addImage(R.drawable.jl_2);
				animation.addImage(R.drawable.jl_3);
				animation.addImage(R.drawable.jl_4);
				animation.addImage(R.drawable.jl_5);
				animation.addImage(R.drawable.jl_6);
				animation.addImage(R.drawable.jl_7);
				animation.addImage(R.drawable.jl_8);
				animation.addImage(R.drawable.jl_9);
				animation.addImage(R.drawable.jl_10);
			}
			if(state.equals("downing"))
			{
				animation.addImage(R.drawable.jl_10);
				animation.addImage(R.drawable.jl_9);
				animation.addImage(R.drawable.jl_8);
				animation.addImage(R.drawable.jl_7);
				animation.addImage(R.drawable.jl_6);
				animation.addImage(R.drawable.jl_5);
				animation.addImage(R.drawable.jl_4);
				animation.addImage(R.drawable.jl_3);
				animation.addImage(R.drawable.jl_2);
				animation.addImage(R.drawable.jl_1);
			}
			break;
		case 3:
			if(state.equals("stop"))
			{
				HardwareManager.AnimationRemove(this);
			}
			if(state.equals("running"))
			{
				animation.addImage(R.drawable.fs_1);
				animation.addImage(R.drawable.fs_2);
				animation.addImage(R.drawable.fs_3);
				animation.addImage(R.drawable.fs_4);
				animation.addImage(R.drawable.fs_5);
				animation.addImage(R.drawable.fs_6);
				animation.addImage(R.drawable.fs_7);
				animation.addImage(R.drawable.fs_8);
				animation.addImage(R.drawable.fs_9);
				animation.addImage(R.drawable.fs_10);
			}
			break;
		case 4:
			if(state.equals("stop"))
			{
				HardwareManager.AnimationRemove(this);
			}
			if(state.equals("running"))
			{
				animation.addImage(R.drawable.dg_1);
				animation.addImage(R.drawable.dg_2);
				animation.addImage(R.drawable.dg_3);
				animation.addImage(R.drawable.dg_4);
				animation.addImage(R.drawable.dg_5);
				animation.addImage(R.drawable.dg_6);
				animation.addImage(R.drawable.dg_7);
				animation.addImage(R.drawable.dg_8);
				animation.addImage(R.drawable.dg_9);
				animation.addImage(R.drawable.dg_10);
			}
			break;
		case 5:
			if(state.equals("stop"))
			{
				HardwareManager.AnimationRemove(this);
			}
			if(state.equals("running"))
			{
				animation.addImage(R.drawable.sb_1);
				animation.addImage(R.drawable.sb_2);
				animation.addImage(R.drawable.sb_3);
				animation.addImage(R.drawable.sb_4);
				animation.addImage(R.drawable.sb_5);
				animation.addImage(R.drawable.sb_6);
				animation.addImage(R.drawable.sb_7);
				animation.addImage(R.drawable.sb_8);
				animation.addImage(R.drawable.sb_9);
				animation.addImage(R.drawable.sb_10);
			}
			break;
		default:
			break;
		}
		if(getAnimation().getAnimationCount()>0 && !state.equals("Stop"))
			HardwareManager.AnimationsAdd(this);
		
	}
	public DrawBackupAnimation getAnimation() {
		return animation;
	}
	
	
	public void setBackgroundView(ImageView backgroundView) {
		animation.setBackgroundView(backgroundView);
	}
	
	public ImageView  getBackgroundView() {
		return animation.getBackgroundView();
	}
	public void setOnButton(ControlButton onBtn)
	{
		onButton = onBtn;
	}
	
	public void  setOffButton(ControlButton offBtn)
	{
		offButton = offBtn;
	}
	
	public void setUpButton(ControlButton upBtn)
	{
		upButton = upBtn;
	}
	
	public void setDownButton(ControlButton downBtn)
	{
		downButton = downBtn;
	}
	
	public void setImageView(ImageView view)
	{
		imageView = view;
	}
	public int getType(){return type;}
	public String getName(){return name;}
	public int getIamge(){return image;}
	public ControlButton getOnButton(){return onButton;}
	public ControlButton getOffButton(){return offButton;}
	public ControlButton getUpButton(){return upButton;}
	public ControlButton getDownButton(){return downButton;}
	
	public void setState(String strState)
	{
		state = strState;
	}
	
	public void bindImageViewByButton(ImageView view,ControlButton button)
	{
		button.setImageView(view);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HardwareManager.setActiveTimer();
				checkState((ImageView)v);
				resetAnimation();
			}


		});
	}
	
	public String getState() {
		return state;
	}
	
	public void ChangeState()
	{
		try
		{
			switch (type) {
			case 1:
			case 2:
				if(this.state.indexOf("stop") >= 0)
				{
				
					if(this.state.indexOf("up") >= 0)
					{
						if(type==1)
						{
							upButton.getImageView().setBackgroundResource(R.drawable.up_stop);
						}
					}
					else
					{
						if(type==1)
						{
							upButton.getImageView().setBackgroundResource(R.drawable.up_off);
						}
						if(type==2)
						{
							upButton.getImageView().setBackgroundResource(R.drawable.open_off);
						}
					}
					if(this.state.indexOf("down") >= 0)
					{
						if(type==1)
						{
							downButton.getImageView().setBackgroundResource(R.drawable.down_stop);
						}
					}
					else {
						if(type==1)
						{
							downButton.getImageView().setBackgroundResource(R.drawable.down_off);
						}
						if(type==2)
						{
							downButton.getImageView().setBackgroundResource(R.drawable.close_off);
						}
						
					}
					offButton.getImageView().setBackgroundResource(R.drawable.off);
					switch (type) {
					case 1:
						switch (getImageGroup()) {
						case 1:
							imageView.setBackgroundResource(R.drawable.jm_1_5);
							break;
						case 2:
							imageView.setBackgroundResource(R.drawable.jm_2_5);
							break;
						case 3:
							imageView.setBackgroundResource(R.drawable.jm_3_5);
							break;
						case 4:
							imageView.setBackgroundResource(R.drawable.jm_4_5);
							break;
						case 5:
							imageView.setBackgroundResource(R.drawable.jm_5_5);
							break;
						case 6:
							imageView.setBackgroundResource(R.drawable.jm_6_1);							
						default:
							break;
						}
						break;
					case 2:
						imageView.setBackgroundResource(R.drawable.jl_10);
						break;
					default:
						break;
					}
				}
				if(this.state.equals("upping"))
				{
					if(type==1)
					{
						upButton.getImageView().setBackgroundResource(R.drawable.up_on);
						downButton.getImageView().setBackgroundResource(R.drawable.down_off);
					}
					if(type==2)
					{
						upButton.getImageView().setBackgroundResource(R.drawable.open_on);
						downButton.getImageView().setBackgroundResource(R.drawable.close_off);
					}
					offButton.getImageView().setBackgroundResource(R.drawable.on);
					switch (type) {
					case 1:
						switch (getImageGroup()) {
						case 1:
							imageView.setBackgroundResource(R.drawable.jm_1_10);
							break;
						case 2:
							imageView.setBackgroundResource(R.drawable.jm_2_10);
							break;
						case 3:
							imageView.setBackgroundResource(R.drawable.jm_3_10);
							break;
						case 4:
							imageView.setBackgroundResource(R.drawable.jm_4_10);
							break;
						case 5:
							imageView.setBackgroundResource(R.drawable.jm_5_10);
							break;
						default:
							break;
						}
						break;
					case 2:
						imageView.setBackgroundResource(R.drawable.jl_10);
						break;
					default:
						break;
					}
				}
				if(this.state.equals("downing"))
				{
					if(type==1)
					{
						upButton.getImageView().setBackgroundResource(R.drawable.up_off);
						downButton.getImageView().setBackgroundResource(R.drawable.down_on);
					}
					if(type==2)
					{
						upButton.getImageView().setBackgroundResource(R.drawable.open_off);
						downButton.getImageView().setBackgroundResource(R.drawable.close_on);
					}
					offButton.getImageView().setBackgroundResource(R.drawable.on);
					switch (type) {
					case 1:
						switch (getImageGroup()) {
						case 1:
							imageView.setBackgroundResource(R.drawable.jm_1_1);
							break;
						case 2:
							imageView.setBackgroundResource(R.drawable.jm_2_1);
							break;
						case 3:
							imageView.setBackgroundResource(R.drawable.jm_3_1);
							break;
						case 4:
							imageView.setBackgroundResource(R.drawable.jm_4_1);
							break;
						case 5:
							imageView.setBackgroundResource(R.drawable.jm_5_1);
							break;
						default:
							break;
						}
						break;
					case 2:
						imageView.setBackgroundResource(R.drawable.jl_1);
						break;
					default:
						break;
					}
				}
				
				break;
			case 3:
			case 4:
			case 5:
				if(this.state.equals("running"))
				{
					onButton.getImageView().setBackgroundResource(R.drawable.on);
					switch (type) {
					case 3:
						imageView.setBackgroundResource(R.drawable.fs_10);
						break;
					case 4:
						imageView.setBackgroundResource(R.drawable.dg_10);
						break;
					case 5:
						imageView.setBackgroundResource(R.drawable.sb_10);
						break;
					default:
						break;
					}
					
				}
				if(this.state.equals("stop"))
				{
					onButton.getImageView().setBackgroundResource(R.drawable.off);
					switch (type) {
					case 3:
						imageView.setBackgroundResource(R.drawable.fs_1);
						break;
					case 4:
						imageView.setBackgroundResource(R.drawable.dg_1);
						break;
					case 5:
						imageView.setBackgroundResource(R.drawable.sb_1);
						break;
					default:
						break;
					}
				}
				
			default:
				break;
			}
		}
		catch(Exception e)
		{
			
		}
	}
	
	private Boolean checkState(ImageView view) {
		String comString = "";
		switch (type) {
		case 1:
		case 2:
			if(offButton.getImageView() == null)
				return false;
			if(upButton.getImageView() == null)
				return false;
			if(downButton.getImageView() == null)
				return false;
			if(state.indexOf("stop") >= 0)
			{
				if(view.equals(offButton.getImageView()))
				{
					state = "stop";
					comString = this.comstop;
				}
				if(view.equals(upButton.getImageView()))
				{
					state = "upping";
					comString = this.comnum;
				}
				if(view.equals(downButton.getImageView()))
				{
					state = "downing";
					comString = this.comdown;
				}
			}
			else if(state.indexOf("up") >= 0)
			{
				if(view.equals(upButton.getImageView()))
				{
					return false;
				}
				if(view.equals(downButton.getImageView()))
				{
					state = "downing";
					comString = this.comdown;
					
				}
				if(view.equals(offButton.getImageView()))
				{
					state = "stop";
					comString = this.comstop;
				}
			}
			else if(state.indexOf("down") >= 0)
			{
				if(view.equals(downButton.getImageView()))
				{
					return false;
				}
				if(view.equals(upButton.getImageView()))
				{
					state = "upping";
					comString = this.comnum;				}
				if(view.equals(offButton.getImageView()))
				{
					state = "stop";
					comString = this.comstop;
				}
			}			
			break;
		case 3:
		case 4:
		case 5:
			if(onButton.getImageView() == null)
				return false;
			if(state.equals("stop"))
			{
				state = "running";
				comString = this.comnum;
			}
			else {
				state = "stop";
				comString = this.comstop;
			}
			break;
		default:
			break;
		}

		ChangeState();
		if(type == 1 || type == 2)
		{
			if(!comString.equals(this.comstop))
			{
				WriteConSensorParams params = new WriteConSensorParams(this, "stop",this.comstop);
				DPWebService.setControlCmd(params);
			}
		}
		
		WriteConSensorParams params = new WriteConSensorParams(this, state,comString);
		DPWebService.setControlCmd(params);
		return true;
	}
	
	public int getImageGroup() {
		return imageGroup;
	}
	
	public void setImageGroup(String imageGroup) {
		this.imageGroup = Integer.parseInt(imageGroup);
	}

	public void Uncode() {
		
		this.ChangeState();
		this.resetAnimation();
	}
	
}
